﻿Imports System
Imports System.Collections.Generic
Imports System.Workflow.ComponentModel
Imports System.Workflow.Runtime

Namespace System.Workflow.Activities
    <Serializable> _
    Friend Class DisableQueuesAction
        Inherits StateMachineAction
        ' Methods
        Friend Sub New(ByVal stateName As String)
            MyBase.New(stateName)
        End Sub

        Friend Overrides Sub Execute(ByVal context As ActivityExecutionContext)
            MyBase.Execute(context)
            Dim rootState As StateActivity = StateMachineHelpers.GetRootState(MyBase.State)
            Dim queue As New Queue([Of] StateActivity)
            queue.Enqueue(rootState)
            Do While (queue.Count > 0)
                Dim activity3 As Activity
                For Each activity3 In queue.Dequeue.EnabledActivities
                    Dim eventDriven As EventDrivenActivity = [TryCast](activity3,EventDrivenActivity)
                    If (Not eventDriven Is Nothing) Then
                        Dim queueName As IComparable = StateMachineHelpers.GetEventActivity(eventDriven).QueueName
                        If (Not queueName Is Nothing) Then
                            Dim workflowQueue As WorkflowQueue = StateMachineSubscriptionManager.GetWorkflowQueue(context, queueName)
                            If (Not workflowQueue Is Nothing) Then
                                workflowQueue.Enabled = MyBase.SubscriptionManager.Subscriptions.ContainsKey(queueName)
                            End If
                        End If
                    Else
                        Dim item As StateActivity = [TryCast](activity3,StateActivity)
                        If (Not item Is Nothing) Then
                            queue.Enqueue(item)
                        End If
                    End If
                Next
            Loop
        End Sub

    End Class
End Namespace

